<?php
require_once("../include/func.php");
require_once("../include/conn.php");

$action = $_REQUEST['action'];
if(empty($action)){
    $action = 'list';
}

echo "action is " .$action;

if ($action == 'new') {
    $title = $_POST['title'];
    $content = $_POST['content'];

    $sql = "insert into blog(title, content, post_date) values('" . $title . "', ' " . $content . "', now())";

    $result = mysql_query($sql, $conn);

    mysql_close($conn);

    if (!$result) {
        echo "error create blog:" . mysql_error();
    } else {
        redirect('../index.php');
    }
} elseif ($action == 'del') {
    $id = $_GET['id'];
    $sql = sprintf("delete from blog where id = %d",$id);

    $result = mysql_query($sql, $conn);
    mysql_close($conn);

    redirect('../index.php');

} elseif ($action == 'mod') {
    $id = $_POST['id'];
    $title = $_POST['title'];
    $content = $_POST['content'];
    $sql = sprintf("update blog set title='%s', content = '%s' where id = %d", $title, $content, $id);
    echo "sql:" . $sql;

    $result = mysql_query($sql, $conn);
    mysql_close($conn);

    if(!$result){
        echo "error update blog:" . mysql_error();
    } else {
        redirect('../index.php');
    }


}


